﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for EditTagBox.xaml
    /// </summary>
    public partial class EditTagBox : Window
    {
        ImageManager manager;

        public EditTagBox(ImageManager manager, MainWindow window)
        {
            this.manager = manager;

            this.Owner = window;

            InitializeComponent();

            List<string> currentTags = this.manager.CurrentTags;

            if (currentTags.Count > 0)
            {
                StringBuilder tagsBuilder = new StringBuilder();

                foreach (string tag in currentTags)
                {
                    tagsBuilder.Append(tag);
                    tagsBuilder.Append(", ");
                }

                string tagsText = tagsBuilder.ToString();

                this.tags.Text = tagsText;
                this.tags.SelectionStart = tagsText.Length;
                this.tags.SelectionLength = 0;
            }

            this.tags.Focus();
        }

        private void tagEditOk_Click(object sender, RoutedEventArgs e)
        {
            string tagString = this.tags.Text;

            string[] tags = tagString.Split(new char[] { ',' });

            List<string> tagsResult = new List<string>();

            string trimTag;
            foreach (string tag in tags)
            {
                trimTag = tag.Trim();
                if (trimTag != "" && !trimTag.Contains("|"))
                {
                    tagsResult.Add(trimTag.ToLowerInvariant());
                }
            }

            this.manager.CurrentTags = tagsResult;
            this.DialogResult = true;
        }
    }
}
